<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="title"
    width="500px"
    @callBack="onCallBack"
    @close="onClose"
  >
    <el-form
      ref="ruleForm"
      :model="form"
      :rules="rules"
      label-width="100px"
      label-position="left"
    >
      <el-form-item label="公司" prop="companyId">
        <el-select
          ref="selectLabel"
          v-model="form.companyName"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词搜素"
          :remote-method="remoteMethod"
          :loading="loading"
          @change="onChange"
          style="width: 100%"
        >
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="{ value: item.id, label: item.name }"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="form.nickName" clearable placeholder="请输入昵称" />
      </el-form-item>

      <el-form-item label="身份证号码" prop="idCard">
        <el-input
          v-model="form.idCard"
          clearable
          placeholder="请输入身份证号码"
        />
      </el-form-item>

      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" clearable placeholder="请输入邮箱" />
      </el-form-item>

      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" clearable placeholder="请输入手机号" />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getOne, register } from "@/api/sysUser";
import { getListByTianYanCha } from "@/api/sysCompany";

export default {
  data() {
    return {
      dialogVisible: false,
      title: "添加",
      loading: false,
      options: [],
      form: {
        id: "",
        companyName: "",
        companyId: "",
        nickName: "",
        idCard: "",
        email: "",
        phone: "",
      },
      rules: {
        companyId: [
          {
            required: true,
            message: "请选择所属公司",
            trigger: "blur",
          },
        ],
        nickName: [
          {
            required: true,
            message: "请输入昵称",
            trigger: "blur",
          },
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: "blur",
          },
        ],
        idCard: [
          {
            required: true,
            message: "请输入身份证号码",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    init() {
      if (this.form.id) {
        getOne(this.form.id).then((res) => {
          if (res && res.code === 20000) {
            const rtn = res.data;
            this.form.id = rtn.id;
            this.form.companyName = rtn.companyName;
            this.form.companyId = rtn.companyId;
            this.form.nickName = rtn.nickName;
            this.form.idCard = rtn.idCard;
            this.form.email = rtn.email;
            this.form.phone = rtn.phone;
          }
        });
      }
    },
    onShow(id) {
      if (id) {
        this.form.id = id;
        this.title = "编辑";
        this.init();
      } else {
        this.title = "添加";
      }
      this.dialogVisible = true;
    },
    onClose() {
      this.$refs["ruleForm"].resetFields();
      this.form = this.$options.data().form;
      this.dialogVisible = false;
    },
    onChange(node) {
      this.form.companyName = node.label;
      this.form.companyId = node.value;
    },
    onSubmit() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          register(this.form)
            .then((res) => {
              if (res && res.code === 20000) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                });
                this.dialogVisible = false;
                this.onCallBack();
              }
            })
            .catch((e) => {});
        } else {
          return false;
        }
      });
    },
    remoteMethod(query) {
      getListByTianYanCha(query).then((res) => {
        if (res.code === 20000) {
          this.options = res.data;
        }
      });
    },
    onCallBack() {
      this.$emit("onCallBack");
    },
  },
};
</script>

<style scoped>
.el-form {
  padding-left: 20px;
  padding-right: 20px;
}
</style>
